<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;

use app\model\CleaningPoint;
use app\model\User;
use think\facade\Request;

class CleaningPointLogic
{

    public function getList()
    {
        $param = Request::get();
        $where = [
            ['cp_status','=',1],
            ['cp_orgid','=',config('user.orgid')]
        ];
        if(isset($param['cleaningPointName']) && !empty($param['cleaningPointName'])){
            $where[] = ['cp_name','like','%'.trim($param['cleaningPointName']).'%'];
        }

        $cleaningPointModel = new CleaningPoint();

        $list = $cleaningPointModel->getPointListByCondition($where);
        return $list;
    }

    public function addPoint()
    {
        $param = Request::post();

        $point = [
            'cp_orgid' => config('user.orgid')
        ];

        if(!isset($param['cleaningPointFloor']) || empty($param['cleaningPointFloor']))   fail('请选择楼层');
        $point['cp_floor'] = intval($param['cleaningPointFloor']);

        if(!isset($param['cleaningPointName']) || empty($param['cleaningPointName']))   fail('请输入保洁点名称');
        $point['cp_name'] = trim($param['cleaningPointName']);

        $cleaningPointModel = new CleaningPoint();
        $pointId = $cleaningPointModel->addOneCleaningPoint($point);

        if(!$pointId)   fail('保洁点添加失败');

        return true;
    }
    public function updatePoint()
    {
        $param = Request::put();

        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $cleaningPointId = intval($param['cleaningPointId']);

        $cleaningPointModel = new CleaningPoint();
        $info = $cleaningPointModel->getPointInfoById($cleaningPointId);
        if(!$info)  fail('保洁点信息不存在');

        $point = [];
        if(!isset($param['cleaningPointFloor']) || empty($param['cleaningPointFloor']))   fail('请选择楼层');
        $point['cp_floor'] = intval($param['cleaningPointFloor']);

        if(!isset($param['cleaningPointName']) || empty($param['cleaningPointName']))   fail('请输入保洁点名称');
        $point['cp_name'] = trim($param['cleaningPointName']);

        $cleaningPointModel = new CleaningPoint();
        $pointUpdate = $cleaningPointModel->updatePointInfoByWhere(['cp_id'=>$cleaningPointId],$point);

        if(!$pointUpdate)   fail('保洁点修改失败');

        return true;
    }


    public function del($cleaningPointId)
    {
        $cleaningPointModel = new CleaningPoint();
        $info = $cleaningPointModel->getPointInfoById($cleaningPointId);


        if(!$info)   fail('保洁点信息不存在');

        if($info['status'] == 0)    fail('保洁点已经删除，不能重复操作');

        $status = [
            'cp_status' => 0,
        ];

        $rt = $cleaningPointModel->updatePointInfoByWhere(['cp_id'=>$cleaningPointId],$status);
        if(!$rt)    fail('操作失败，请稍后重试');

        return true;
    }

}